// 新增页面子表头部操作按钮组件

import React, {Component} from 'react';

import './index.less';

import {toast, base} from 'nc-lightapp-front';

import language from '../../../../language';

const {
    NCButton
} = base;

class TableOperator extends Component {
    constructor(props) {
        super(props);

        this.state = {
            buttonDisable: false
        };

        this.clickHandle = this.clickHandle.bind(this);
    }

    // 子表肩部按钮回调函数
    clickHandle(btnCode) {
        return () => {
            const {
                cardTable: {
                    getCheckedRows
                },
                moduleId,
                deleteRow,
                copyRow,
                addRow
            } = this.props;
    
            // 可批量操作，所以是操作所有选中的数据
            const selected = getCheckedRows(moduleId);
    
            // 增加按钮
            if(btnCode === 'AddLine') {
                addRow(moduleId, undefined, {}, false);
            }
            // 删除按钮
            else if(btnCode === 'DelLine') {
                let selectedIndex = selected.map((item) => {
                    return item.index;
                });
                deleteRow(moduleId, selectedIndex);
            }
            // 复制按钮，后来复制按钮被删除掉了，只保留行上的复制操作
            else if(btnCode === 'CopyLine') {
                if(selected.length <= 0) {
                    toast({
                        color: 'warning',
                        content: language['hi6007-000170']/* 国际化处理： 请选择复制的行*/
                    });
                }
                else {
                    let selectedData = [];
                    selected.map((item) => {
                        selectedData.push(item.data);
                    });
                    copyRow(moduleId, selectedData);
                }
            }
        }
    }

    componentDidMount() {
    
    }

    render() {
        const {
            button: {
                createButtonApp
            },
            subTableEditable, // 代表子表是否可编辑
            pageFrom,
            moduleId
        } = this.props;

        const {
            buttonDisable
        } = this.state;

        let addButtonDisable = pageFrom === 'modify' && moduleId === 'hi_psnjob';

        return (
            <div class="add-new-page-table-operator">
                {
                    subTableEditable && 
                    <div>
                        <NCButton
                            colors="normal"
                            onClick={this.clickHandle('AddLine')}
                            disabled={buttonDisable || addButtonDisable}
                        >
                            {language['hi6007-000083']/* 增加 */}
                        </NCButton>
                        <NCButton
                            colors="normal"
                            onClick={this.clickHandle('DelLine')}
                            disabled={buttonDisable}
                        >
                            {language['hi6007-000031']/* 删除 */}
                        </NCButton>
                        
                    </div>
                }
            </div>
        );
    }
}
/**
 * 产品突然又说不要复制功能了，为了以后他们反悔所以还是保存下吧
 * <NCButton
    colors="normal"
    onClick={this.clickHandle('CopyLine')}
    disabled={buttonDisable}
>
    {language['hi6007-000054']}
    </NCButton>
 * 
 *  */
export default TableOperator;
